﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Area
{
    public class DetailsAreaModel
    {
        public int AreaId { get; set; }
        public string Name { get; set; }
        public string Management { get; set; }
        public string Process { get; set; }

        private riskanalysisEntities entities = new riskanalysisEntities();

        public DetailsAreaModel(int areaId)
        {
            var query = from a in entities.areas
                        where a.areaId == areaId && a.enabled
                        select new
                        {
                            AreaId = a.areaId,
                            Name = a.name,
                            Management = a.process.management.name,
                            Process = a.process.name
                        };

            var theArea = query.FirstOrDefault();

            if (theArea == null)
                throw new DetailsAreaException("El area especificada no existe");

            AreaId = theArea.AreaId;
            Name = theArea.Name;
            Management = theArea.Management;
            Process = theArea.Process;
        }

        public void DeleteManagement()
        {
            var managementList = from a in entities.areas
                                 where a.areaId == AreaId && a.enabled
                                 select a;

            if (managementList.Count() <= 0)
                throw new DetailsAreaException("El area especificada no fue encontrada");

            managementList.First().enabled = false;
            entities.SaveChanges();
        }
    }
}